package com.example.zsfinacialmanage.view;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.constant.MessageConstant;
import com.example.zsfinacialmanage.utils.MyDBHelper;
import com.example.zsfinacialmanage.utils.Md5Util;

public class RecoverActivity extends AppCompatActivity {


    private EditText et_name,et_newPwd,et_phone,et_email;
    private Button btn_ok,btn_back;
    private MyDBHelper myDBHelper;//创建一个数据库类的文件
    private SQLiteDatabase db;//创建一个操作数据库的对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recover);
        //初始划
        initView();
        //修改密码功能实现
        btnOk();
        //返回按钮功能实现
        btnBack();
    }

    private void btnBack() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RecoverActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @SuppressLint("Range")
    private void btnOk() {
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1.获取用户输入的信息
                String inputName = et_name.getText().toString();
                String inputNewPwd = et_newPwd.getText().toString();
                String inputPhone = et_phone.getText().toString();
                String inputEmail = et_email.getText().toString();
                String inputNewPwdMd5 = Md5Util.get(inputNewPwd);

                //2.判断是否为空
                if (inputName.equals("") || inputNewPwd.equals("") || inputPhone.equals("") || inputEmail.equals("")){
                    Toast.makeText(RecoverActivity.this, MessageConstant.USERINFO_NOT_NULL,Toast.LENGTH_SHORT).show();
                }else {

                    //判断用户是否存在
                    Cursor cursorName;
                    String queryName = "SELECT * FROM tb_userinfo WHERE name=?";
                    cursorName = db.rawQuery(queryName, new String[]{inputName});
                    if (cursorName != null && cursorName.getCount() > 0){
                        //判断用户电话是否正确
                        Cursor cursorPhone;
                        String queryPhone = "SELECT * FROM tb_userinfo WHERE phone=?";
                        cursorPhone = db.rawQuery(queryPhone, new String[]{inputPhone});
                        if (cursorPhone != null && cursorPhone.getCount() > 0){
                            //账号、电话正确，修改密码
                            ContentValues values =new ContentValues();
                            values.put("pwd",inputNewPwdMd5);
                            db.update("tb_userinfo",values,"name = ? and phone = ?",new String[]{inputName,inputPhone});
                            //修改密码完成检查是否修改成功
                            String getPwdMd5 = null;
                            Cursor cursor;
                            String query = "SELECT pwd FROM tb_userinfo WHERE name = ?";
                            cursor = db.rawQuery(query, new String[]{inputName});
                            if (cursor != null && cursor.moveToFirst()) {
                                getPwdMd5 = cursor.getString(cursor.getColumnIndex("pwd"));
                                cursor.close();
                                if (inputNewPwdMd5.equals(getPwdMd5)){
                                    Toast.makeText(RecoverActivity.this, MessageConstant.PASSWORD_EDIT_SUCCESS,Toast.LENGTH_SHORT).show();
                                    //修改密码成功后返回登录页面
                                    Intent intent = new Intent(RecoverActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    finish();
                                }else {
                                    Toast.makeText(RecoverActivity.this, MessageConstant.PASSWORD_EDIT_FAIL,Toast.LENGTH_SHORT).show();
                                }
                            }else {
                                Toast.makeText(RecoverActivity.this, MessageConstant.PASSWORD_EDIT_FAIL,Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            Toast.makeText(RecoverActivity.this,MessageConstant.PHONE_ERROR,Toast.LENGTH_SHORT).show();
                        }
                        cursorName.close();
                    }else {
                        Toast.makeText(RecoverActivity.this,inputName + MessageConstant.NOT_EXISTS,Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
    }

    private void initView() {
        et_name = findViewById(R.id.et_name_rec);
        et_newPwd = findViewById(R.id.et_newpwd_rec);
        et_phone = findViewById(R.id.et_phone_rec);
        et_email = findViewById(R.id.et_email_rec);
        btn_ok = findViewById(R.id.bt_ok_rec);
        btn_back = findViewById(R.id.bt_back_rec);
        myDBHelper = new MyDBHelper(RecoverActivity.this);
        db = myDBHelper.getWritableDatabase();
    }
}